
package com.cskaoyan.service;
import com.cskaoyan.bean.FloorGoods;
import com.cskaoyan.domain.*;
import com.cskaoyan.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class HomeIndexImpl implements HomeIndex {
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketSystemMapper systemMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public Map<String, Object> getIndex() {
        Map<String, Object> map = new HashMap<>();
        //获取banner
        List<MarketAd> bannerList = marketAdMapper.selectByExample(new MarketAdExample());
        map.put("banner", bannerList);
        //获取brandList
        String keyValue1 = systemMapper.selectKeyNameAndKeyValue("market_wx_index_brand");
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setOrderByClause("add_time");
        List<MarketBrand> brandList = brandMapper.selectByExample(marketBrandExample);
        List<MarketBrand> realBrandList = brandList.stream().limit(Integer.parseInt(keyValue1)).collect(Collectors.toList());
        map.put("brandList", realBrandList);
        //获取channel
        String keyValue2 = systemMapper.selectKeyNameAndKeyValue("market_wx_catlog_list");
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.setOrderByClause("add_time");
        List realChannelList= com.cskaoyan.utils.ListLimit.limitList(marketCategoryMapper.selectByExampleSelective(marketCategoryExample),keyValue2);
        map.put("channel", realChannelList);
        //获取couponList
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.setOrderByClause("add_time");
        List realCouponList = com.cskaoyan.utils.ListLimit.limitList(marketCouponMapper.selectByExampleSelective(marketCouponExample),"3");
        map.put("couponList",realCouponList);
        //获取hotGoodsList
        String keyValue3 = systemMapper.selectKeyNameAndKeyValue("market_wx_index_hot");
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIsHotEqualTo(true);
        List realHotGoodsList = com.cskaoyan.utils.ListLimit.limitList(marketGoodsMapper.selectByExampleSelective(marketGoodsExample),keyValue3);
        map.put("hotGoodsList",realHotGoodsList);
        //获取newGoodsList
        String keyValue4 = systemMapper.selectKeyNameAndKeyValue("market_wx_index_new");
        MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
        criteria1.andIsNewEqualTo(true);
        List realNewGoodsList = com.cskaoyan.utils.ListLimit.limitList(marketGoodsMapper.selectByExampleSelective(marketGoodsExample1),keyValue4);
        map.put("newGoodsList",realNewGoodsList);
        //获取topicList
        String keyValue5 = systemMapper.selectKeyNameAndKeyValue("market_wx_index_topic");
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.setOrderByClause("add_time");
        List realTopicList = com.cskaoyan.utils.ListLimit.limitList(marketTopicMapper.selectByExampleSelective(marketTopicExample), keyValue5);
        map.put("topicList",realTopicList);
        //floorGoodsList
        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExampleSelective(marketCategoryExample1);
        Collections.shuffle(marketCategories);
        List<MarketCategory> categoryList = com.cskaoyan.utils.ListLimit.limitList(marketCategories, keyValue2);
        List<FloorGoods>arrayList=new ArrayList<>();
        for (MarketCategory marketCategory : categoryList) {
            MarketGoodsExample marketGoodsExample2 = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria2 = marketGoodsExample2.createCriteria();
            criteria2.andCategoryIdEqualTo(marketCategory.getId());
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExampleSelective(marketGoodsExample2);
            List<MarketGoods> list = com.cskaoyan.utils.ListLimit.limitList(marketGoods, keyValue2);
            FloorGoods floorGoods = new FloorGoods();
            floorGoods.setId(marketCategory.getId());
            floorGoods.setName(marketCategory.getName());
            floorGoods.setGoodsList(list);
            arrayList.add(floorGoods);
        }
        map.put("floorGoodsList",arrayList);
        return map;

    }
    private List<Map> getCategoryList() {
        List<Map> categoryList = new ArrayList<>();
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = example.createCriteria();
        criteria1.andLevelEqualTo("L1");
        List<MarketCategory> catL1List = marketCategoryMapper.selectByExample(example);
        for (MarketCategory catL1 : catL1List) {
            MarketCategoryExample example1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria2 = example1.createCriteria();
            criteria2.andPidEqualTo(catL1.getId());
            List<MarketCategory> catL2List = marketCategoryMapper.selectByExample(example1);
            List<MarketGoods> categoryGoods;
            if (catL2List.size() == 0) {
                categoryGoods = new ArrayList<>();
            } else {
                String keyValue6 = systemMapper.selectKeyNameAndKeyValue("market_wx_catlog_goods");
                categoryGoods = com.cskaoyan.utils.ListLimit.limitList(catL2List,keyValue6);
            }
            Map<String, Object> catGoods = new HashMap<>();
            catGoods.put("id", catL1.getId());
            catGoods.put("name", catL1.getName());
            catGoods.put("goodsList", categoryGoods);
            categoryList.add(catGoods);
        }
        return categoryList;
    }
}

